package com.linghu.api;

import com.linghu.entity.Comment;
import com.linghu.entity.Teacher;
import com.linghu.resp.Result;
import com.linghu.service.CommentService;
import com.linghu.utils.CommentUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.time.Instant;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author linghu
 * @date 2024/4/22 12:07
 */
@RestController
@RequestMapping("/query")
public class CommentsApi {

    @Resource
    private CommentService commentService;

    /**
     * @Description: 获取所有的评论信息
     * @param
     * @return: com.linghu.resp.Result<?>
     * @Author: codelinghu
     * @Date: 2024/4/22
     */
    @GetMapping("/allComments")
    @ResponseBody
    public Result<?> QueryAllComments(){
        //获取所有评论
        List<Comment> lists = commentService.list();
        List<Comment> commentList = CommentUtils.processComments(lists);
        return Result.success(commentList);
    }


    @PostMapping("/subComments")
    @ResponseBody
    public Result<?> SubmittedAllComments(@RequestBody Comment comment){
        // 获取当前UTC时间的Date对象
        comment.setCreateTime(Date.from(Instant.now()));
        //插入评论
        boolean save = commentService.save(comment);
        if (save){
            return Result.success();
        }
        return Result.error("400","error");
    }

}
